package com.glorysoft.xztpda.ui;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glorysoft.xztpda.base.Constant;
import com.glorysoft.xztpda.ui.login.LoginActivity;
import com.glorysoft.xztpda.utils.MyUtils;
import com.glorysoft.xztpda.R;
import com.glorysoft.xztpda.base.DataBindBaseViewHolder;
import com.glorysoft.xztpda.databinding.ItemMainBinding;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabIndexGridEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<FreeUI_TabIndexGridEntity> mfgItems;
    private MainAdapter mAdapter;
    private MainAdapter mFTAdapter;
    private MainAdapter mOtherAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView rvMFGList = findViewById(R.id.rvMFGList);
        RecyclerView rvFTList = findViewById(R.id.rvFTList);
        RecyclerView rvOtherList = findViewById(R.id.rvOtherList);
        ImageView ivLoginOut = findViewById(R.id.ivLoginOut);

        mfgItems = new ArrayList<>();
        mAdapter = new MainAdapter(null);
        mFTAdapter = new MainAdapter(null);
        mOtherAdapter = new MainAdapter(null);

        rvMFGList.setAdapter(mAdapter);
        rvFTList.setAdapter(mFTAdapter);
        rvOtherList.setAdapter(mOtherAdapter);
        initAssets();
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent();
            FreeUI_TabIndexGridEntity entity = mAdapter.getData().get(position);
            try {
                Class<?> clazz = Class.forName(entity.getSkipClass());
                intent.putExtra("showPlugin", entity.getShowPlugin());
                intent.setClass(MainActivity.this, clazz);
                startActivity(intent);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        });

        mFTAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent();
            FreeUI_TabIndexGridEntity entity = mFTAdapter.getData().get(position);
            try {
                Class<?> clazz = Class.forName(entity.getSkipClass());
                intent.putExtra("showPlugin", entity.getShowPlugin());
                intent.setClass(MainActivity.this, clazz);
                startActivity(intent);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        });

        mOtherAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent();
            FreeUI_TabIndexGridEntity entity = mOtherAdapter.getData().get(position);
            try {
                Class<?> clazz = Class.forName(entity.getSkipClass());
                intent.putExtra("showPlugin", entity.getShowPlugin());
                intent.setClass(MainActivity.this, clazz);
                startActivity(intent);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        });

        ivLoginOut.setOnClickListener(v -> FreeApi_DialogUtils.getConfirmAndCancelDialog(MainActivity.this, getString(R.string.confirm),
                getString(R.string.cancel),"确定退出登录？", () ->{
                    Constant.Companion.getAUTHORITYLIST().clear();
                    Constant.Companion.setCURRENT_USER("");
                    startActivity(new Intent(this, LoginActivity.class));
                    finish();
                }
                , () -> {
                }).show(getSupportFragmentManager(),"LOGIN_OUT"));
    }

    private void initAssets() {
        try {
            String fileName = "mfg.txt";
            InputStream mfgJsonStream = getResources().getAssets().open(fileName);
            InputStream ftJsonStream = getResources().getAssets().open("ft.txt");
            InputStream otherJsonStream = getResources().getAssets().open("other.txt");
            String mfgJson = FreeApi_StringUtils.getString(mfgJsonStream);
            String ftJson = FreeApi_StringUtils.getString(ftJsonStream);
            String otherJson = FreeApi_StringUtils.getString(otherJsonStream);
            List<FreeUI_TabIndexGridEntity> mfgEntity = filteringData(new Gson().fromJson(mfgJson,
                    new TypeToken<List<FreeUI_TabIndexGridEntity>>() {
                    }.getType()));
            List<FreeUI_TabIndexGridEntity> ftEntity = filteringData(new Gson().fromJson(ftJson,
                    new TypeToken<List<FreeUI_TabIndexGridEntity>>() {
                    }.getType()));
            List<FreeUI_TabIndexGridEntity> otherEntity = filteringData(new Gson().fromJson(otherJson,
                    new TypeToken<List<FreeUI_TabIndexGridEntity>>() {
                    }.getType()));
            mAdapter.setNewData(MyUtils.INSTANCE.setPicToDrawable(this,mfgEntity));
            mFTAdapter.setNewData(MyUtils.INSTANCE.setPicToDrawable(this,ftEntity));
            mOtherAdapter.setNewData(MyUtils.INSTANCE.setPicToDrawable(this,otherEntity));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private List<FreeUI_TabIndexGridEntity> filteringData(List<FreeUI_TabIndexGridEntity> entity){
        Iterator<FreeUI_TabIndexGridEntity> it = entity.iterator();
        while (it.hasNext()) {
            FreeUI_TabIndexGridEntity next = it.next();

            try {
                Class<?> aClass = Class.forName(next.getSkipClass());
                String name = aClass.getSimpleName();
                if (!Constant.Companion.getAUTHORITYLIST().contains(name)) {
                    it.remove();
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        }
        return entity;
    }

    private class MainAdapter extends BaseQuickAdapter<FreeUI_TabIndexGridEntity, DataBindBaseViewHolder> {

        public MainAdapter(@Nullable List<FreeUI_TabIndexGridEntity> data) {
            super(R.layout.item_main,data);
        }

        @Override
        protected void convert(DataBindBaseViewHolder helper, FreeUI_TabIndexGridEntity item) {
            ItemMainBinding binding = (ItemMainBinding) helper.getDataBinding();
            binding.setTabIndexGrid(item);
            binding.executePendingBindings();
        }
    }
}